package com.cskaoyan.service;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.MarketKeywordExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminKeywordServiceImpl implements AdminKeywordService {

    @Override
    public CommonData query(Integer page, Integer limit, String sort, String order) {
        //？
        MarketKeywordMapper marketKeywordMapper = MyBatisUtil.getMapper(MarketKeywordMapper.class);
        PageHelper.startPage(page, limit);   //分页
        MarketKeywordExample example = new MarketKeywordExample();
        example.setOrderByClause(sort + " " + order); //排序

        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(example);
        PageInfo<MarketKeyword> pageInfo = new PageInfo<>(marketKeywords);
        return CommonData.data(pageInfo);
    }

    @Override  //通过关键字keyword 模糊查询
    public List<MarketKeyword> selectByKeyword(String keyword) {
        //下面两句 -----------？
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper marketKeywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);

        List<MarketKeyword> marketKeywordList = marketKeywordMapper.selectByKeyword(keyword);
        return marketKeywordList;
    }

    @Override  //通过url 精准查询
    public MarketKeyword selectByUrl(String url) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper marketKeywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);

        MarketKeyword marketKeyword = marketKeywordMapper.selectByUrl(url);
        return  marketKeyword;
    }

    @Override //-----------------------老师帮忙写的-------------------------------
    public CommonData query(Integer page, Integer limit, String sort, String order, String keyword, String url) {
       //========================下面两句作用
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper marketKeywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);

        // 分页
        PageHelper.startPage(page, limit);

        MarketKeywordExample example = new MarketKeywordExample();
        // 排序
        example.setOrderByClause(sort + " " + order);
        // 条件的集合
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        if (keyword != null && keyword.length() != 0) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (url != null && url.length() > 0) {
            criteria.andUrlEqualTo(url);
        }


        List<MarketKeyword> marketKeywordList = marketKeywordMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketKeywordList);
        return CommonData.data(pageInfo);
    }

    @Override
    public void insert(MarketKeyword marketKeyword) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper mapper = sqlSession.getMapper(MarketKeywordMapper.class);

        mapper.insertSelective(marketKeyword);
        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public void update(MarketKeyword marketKeyword) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper marketKeywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);


     /*   MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();


        // 数据库的updatetime
        criteria.andUpdateTimeEqualTo(marketKeyword.getUpdateTime());

      // 页面的 keyword   url   ishot  isdefault
        criteria.andKeywordEqualTo(marketKeyword.getKeyword());
        criteria.andUrlEqualTo(marketKeyword.getUrl());
        criteria.andIsHotEqualTo(marketKeyword.getIsHot());
        criteria.andIsDefaultEqualTo(marketKeyword.getIsDefault());

        marketKeywordMapper.updateByExample(marketKeyword,example);*/

        //通过主键来更新
        marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);

        sqlSession.commit();


    }

    @Override
    public int delete(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper mapper = sqlSession.getMapper(MarketKeywordMapper.class);

        int i = mapper.deleteByPrimaryKey(id);
        sqlSession.commit();
        return i;

    }


}



